package com.example.musicplatform.controller;

import com.example.musicplatform.common.Constants;
import com.example.musicplatform.entity.User;
import com.example.musicplatform.pojo.ResponseData;
import com.example.musicplatform.service.LoginService;
import com.example.musicplatform.util.ResponseDataUtil;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;


/**
 * @Author: caowj1217
 * @Date: 2021/7/23下午1:39
 * @Description:
 */
@Controller
public class LoginController {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(LoginController.class);

    @Autowired
    LoginService loginService;

    @GetMapping(value = {"/","/login"})
    public String loginPage(){
        return "login";
    }

    @PostMapping(value = "/login")
    @ResponseBody
    public ResponseData login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord, HttpServletRequest request){
        User user = new User();
        user.setUserName(userName);
        user.setPassWord(passWord);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        HttpSession session = request.getSession(true);
        session.setAttribute("user", user);
        return loginService.findUserByUserNameAndPassWord(user);
    }

    @GetMapping(value = "/main")
    public ResponseData main(){
        return ResponseDataUtil.buildSuccess();
    }



}
